<template>
    <div>
        <el-dialog :title="title" :visible.sync="addVisible" :close-on-click-modal="false"
            :close-on-press-escape="false" @closed="handlerCancel()">
            <el-form :model="menuInfo">
                <el-form-item label="id" :label-width="formLabelWidth">
                    <el-input v-model="menuInfo.id" :disabled="!isadd"></el-input>
                </el-form-item>
                <el-form-item label="名称" :label-width="formLabelWidth">
                    <el-input v-model="menuInfo.label"></el-input>
                </el-form-item>
                <el-form-item label="父级id" :label-width="formLabelWidth">
                    <el-input v-model="menuInfo.parentId"></el-input>
                </el-form-item>
                <el-form-item label="父级名称" :label-width="formLabelWidth">
                    <el-select v-model="selValue" placeholder="请选择" @change="handlerChange">
                        <el-option :key="0" label="顶级菜单" :value="0">
                        </el-option>
                        <el-option v-for="item in ones" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="授权标识码" :label-width="formLabelWidth">
                    <el-input v-model="menuInfo.code"></el-input>
                </el-form-item>
                <el-form-item label="路由名称" :label-width="formLabelWidth">
                    <el-input v-model="menuInfo.name"></el-input>
                </el-form-item>
                <el-form-item label="路由地址" :label-width="formLabelWidth">
                    <el-input v-model="menuInfo.url"></el-input>
                </el-form-item>
                <el-form-item label="权限类型" :label-width="formLabelWidth">
                    <el-input v-model="menuInfo.type"></el-input>
                </el-form-item>
                <el-form-item label="图标" :label-width="formLabelWidth">
                    <el-input v-model="menuInfo.icon"></el-input>
                </el-form-item>
                <el-form-item label="备注" :label-width="formLabelWidth">
                    <el-input v-model="menuInfo.remark"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="handlerCancel()">取 消</el-button>
                <el-button type="primary" @click="handlerSave()">保 存</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { menuInfByIdoApi, addMenuApi, editMenuApi, oneMenusApi } from '../../api/MenuApi'
export default {
    props: ['addVisible', "title"],
    data() {
        return {
            menuInfo: {
                id: "",
                label: "",
                parentId: 0,
                parentName: "",
                code: "",
                name: "",
                url: "",
                type: 0,
                icon: "",
                remark: null,
            },
            isadd: false,
            ones: [],
            selValue: '',
            formLabelWidth: '120px',
            options: {
                "0": '顶级菜单',
            },
        };
    },
    mounted() {
        this.loadMenuS();
    },
    methods: {
        async loadMenuS() {
            try {
                const res = await oneMenusApi();
                if (res.status === 200) {
                    this.ones = res.data.map((item) => {
                        this.options[item.id] = item.label;
                        return { value: item.id, label: item.label };
                    })
                } else {
                    this.$message.error(res.msg);
                }
            } catch (error) {
                console.error('加载菜单列表失败:', error);
                this.$message.error('网络错误，请稍后重试');
            }
        },
        handlerCancel() {
            this.$emit('closeDialog');

        },
        async handlerSave() {
            if (this.isadd) {
                try {
                    const res = await addMenuApi(this.menuInfo);
                    if (res.status === 200) {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1000,
                            onClose: () => {
                                this.$emit('closeDialog', true);
                            }
                        });

                    } else {
                        this.$message.error(res.msg);
                    }
                } catch (error) {
                    console.error('获取用户列表失败:', error);
                    this.$message.error('网络错误，请稍后重试');
                }

            } else {
                try {
                    const res = await editMenuApi(this.menuInfo);
                    if (res.status === 200) {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1000,
                            onClose: () => {
                                this.$emit('closeDialog', true);
                            }
                        });

                    } else {
                        this.$message.error(res.msg);
                    }
                } catch (error) {
                    console.error('获取用户列表失败:', error);
                    this.$message.error('网络错误，请稍后重试');
                }

            }

        },

        async openDialog(menuId) {
            if (!menuId) {
                this.menuInfo = {
                    id: "",
                    label: "",
                    parentId: 0,
                    parentName: "",
                    code: "",
                    name: "",
                    url: "",
                    type: 0,
                    icon: "",
                    remark: null,
                };
                this.isadd = true;
                this.selValue = 0;
            } else {
                try {
                    this.isadd = false;
                    const res = await menuInfByIdoApi(menuId);
                    if (res.status === 200) {
                        this.menuInfo = res.data;
                        this.selValue = res.data.parentId;
                    } else {
                        this.$message.error(res.msg);
                    }
                } catch (error) {
                    console.error('获取用户列表失败:', error);
                    this.$message.error('网络错误，请稍后重试');
                }


            }
        },
        handlerChange(value) {   
            this.menuInfo.parentId = value;
            this.menuInfo.parentName = this.options[value];
        }


    }
};

</script>
<style scoped lang="less">
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>